﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using Catering.View_Operaction;
using Catering_Rules;
using System.Data;
using System.Text;

namespace Catering.Catering_Page.ShanShiFenXi
{
    public partial class PersonEdible_Statistics : System.Web.UI.Page
    {
        // 属性注入
        private static View_Monitor vm = new View_Monitor();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ReportViewer1.AsyncRendering = false;
                this.ReportViewer1.SizeToReportContent = false;
                this.ReportViewer1.ZoomMode = ZoomMode.Percent;
            }
        }

        protected void lbtn_Click(object sender, EventArgs e)
        {
            string AREAID = UCDDLToolWithYearOfMonth1.GetDDL_Area();
            string CLASSID = UCDDLToolWithYearOfMonth1.GetDDL_Class();
            DateTime DTIME = UCDDLToolWithYearOfMonth1.GetDateTime();
            string TimeType = UCDDLToolWithYearOfMonth1.GetTimeType();

            switch (TimeType)
            {
                case "Year":
                    GetXiaohaoSumByYear(AREAID, CLASSID, DTIME);
                    break;
                case "Month":
                    GetXiaohaoSumByMonth(AREAID, CLASSID, DTIME);
                    break;
            }
        }

        private void GetXiaohaoSumByYear(string AREAID, string CLASSID, DateTime DTIME)
        {
            // 报表的数据源
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(AREAID, CLASSID, DTIME, "食用油人均消耗");
            int companyid = 0;
            if (Session["UserID"] != null)
            {
                string userid = Session["UserID"].ToString();
                companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
            }
            ReportDataSource rds = new ReportDataSource("DataSet1", vm.GetPersonOilAvgForYear(companyid, AREAID, CLASSID, DTIME, Title));

            // 先清除报表数据源
            ReportViewer1.LocalReport.DataSources.Clear();

            // 绑定数据源
            ReportViewer1.LocalReport.DataSources.Add(rds);

            // 呈现出数据
            ReportViewer1.LocalReport.Refresh();
            ReportViewer1.Visible = true;
        }

        private void GetXiaohaoSumByMonth(string AREAID, string CLASSID, DateTime DTIME)
        {
            // 报表的数据源
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(AREAID, CLASSID, DTIME, "食用油人均消耗");
            int companyid = 0;
            if (Session["UserID"] != null)
            {
                string userid = Session["UserID"].ToString();
                companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
            }
            ReportDataSource rds = new ReportDataSource("DataSet1", vm.GetPersonOilAvgForYear(companyid, AREAID, CLASSID, DTIME, Title));

            // 先清除报表数据源
            ReportViewer1.LocalReport.DataSources.Clear();

            // 绑定数据源
            ReportViewer1.LocalReport.DataSources.Add(rds);

            // 呈现出数据
            ReportViewer1.LocalReport.Refresh();
            ReportViewer1.Visible = true;
        }

        protected void lbtnChar_Click(object sender, EventArgs e)
        {
            // 先清除报表数据源
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.Visible = false;
            string AREAID = UCDDLToolWithYearOfMonth1.GetDDL_Area();
            string CLASSID = UCDDLToolWithYearOfMonth1.GetDDL_Class();
            DateTime DTIME = UCDDLToolWithYearOfMonth1.GetDateTime();
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            // 报表的数据源
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(AREAID, "-10", DTIME, "人均用油对比图");
            DataTable frdt = vm.GetPersonOilAvgGroupByClassID(pid, AREAID, CLASSID, DTIME);
            Dictionary<string, decimal> list = GetReportModel(AREAID, CLASSID);//班组列表(x轴列表)
            if (frdt != null && frdt.Rows.Count > 0)
            {
                for (int i = 0; i < frdt.Rows.Count; i++)
                {
                    int classid = Convert.ToInt32(frdt.Rows[i]["ClassID"]);
                    string className = frdt.Rows[i]["CName"].ToString();
                    decimal avgoil = Convert.ToDecimal(frdt.Rows[i]["AvgOil"]);
                    if (list.ContainsKey(className))
                    {
                        list[className] = avgoil;
                    }
                }
                frdt.Dispose();
            }

            string StrX = "";
            string str = "";
            if (list != null)
            {
                StringBuilder Sresult = new StringBuilder("");
                Sresult.Append("{");
                Sresult.Append("name:'人均用油',");
                Sresult.Append("data: [");
                string temstr = "";
                foreach (string key in list.Keys)
                {
                    StrX += "'" + key + "',";
                    temstr += list[key] + ",";

                }
                temstr = temstr.TrimEnd(',');
                Sresult.Append(temstr + "]");
                Sresult.Append("}");
                StrX = StrX.TrimEnd(',');
                str = Sresult.ToString();
            }
            StrX = "[" + StrX + "]";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('1','" + Title + "'," + StrX + "," + str + ");", true);
        }

        protected void lbtnClassChar_Click(object sender, EventArgs e)
        {
            // 先清除报表数据源
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.Visible = false;
            string AREAID = UCDDLToolWithYearOfMonth1.GetDDL_Area();
            DateTime DTIME = UCDDLToolWithYearOfMonth1.GetDateTime();
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            // 报表的数据源
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(AREAID, "-10", DTIME, "人均消耗值");
            DataTable frdt = vm.GerPersonOilAvgGroupByGroupID(pid, AREAID, DTIME);
            Dictionary<string, decimal> list = GetReportAreaModel(AREAID);//作业区列表(x轴列表)
            if (frdt != null && frdt.Rows.Count > 0)
            {
                for (int i = 0; i < frdt.Rows.Count; i++)
                {
                    int classid = Convert.ToInt32(frdt.Rows[i]["AreaID"]);
                    string className = frdt.Rows[i]["AreaName"].ToString();
                    decimal avgoil = Convert.ToDecimal(frdt.Rows[i]["AvgOil"]);
                    if (list.ContainsKey(className))
                    {
                        list[className] = avgoil;
                    }
                }
                frdt.Dispose();
            }

            string StrX = "";
            string str = "";
            if (list != null)
            {
                StringBuilder Sresult = new StringBuilder("");
                Sresult.Append("{");
                Sresult.Append("name:'人均用油',");
                Sresult.Append("data: [");
                string temstr = "";
                foreach (string key in list.Keys)
                {
                    StrX += "'" + key + "',";
                    temstr += list[key] + ",";

                }
                temstr = temstr.TrimEnd(',');
                Sresult.Append(temstr + "]");
                Sresult.Append("}");
                StrX = StrX.TrimEnd(',');
                str = Sresult.ToString();
            }
            StrX = "[" + StrX + "]";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('2','" + Title + "'," + StrX + "," + str + ");", true);
        }

        /// <summary>
        /// 获取横坐标值
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public Dictionary<int, string> GetClassList(string groupid, string classid)
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            if (!string.IsNullOrEmpty(groupid) && !groupid.Equals("-10"))
            {
                if (!string.IsNullOrEmpty(classid) && !classid.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(classid), UCDDLToolWithYearOfMonth1.GetAllDDL_Class()[Convert.ToInt32(classid)]);
                    return result;
                }
                else
                {
                    return UCDDLToolWithYearOfMonth1.GetAllDDL_Class();
                }
            }
            else
            {
                Dictionary<int, string> temp = UCDDLToolWithYearOfMonth1.GetAllDDL_Area();
                List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetAllList(0);
                if (temp.Count > 0)
                {
                    foreach (int id in temp.Keys)
                    {
                        foreach (Catering_Model.ClassInfo item in listClass.Where(c => c.AreaID == id))
                        {
                            result.Add(item.id, item.CName);
                        }
                    }
                }
            }
            return result;
        }

        public Dictionary<int, string> GetAreaList(string groupid)
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            if (!string.IsNullOrEmpty(groupid) && !groupid.Equals("-10"))
            {
                result.Add(Convert.ToInt32(groupid), UCDDLToolWithYearOfMonth1.GetAllDDL_Area()[Convert.ToInt32(groupid)]);
                return result;
            }
            else
            {
                result = UCDDLToolWithYearOfMonth1.GetAllDDL_Area();

            }
            return result;
        }

        /// <summary>
        /// 获取横坐标值,并初始化
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public Dictionary<string, decimal> GetReportModel(string groupid, string classid)
        {
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            Dictionary<int, string> temp = GetClassList(groupid, classid);
            foreach (string value in temp.Values)
            {
                if (!result.ContainsKey(value)) result.Add(value, 0);
            }
            return result;
        }

        public Dictionary<string, decimal> GetReportAreaModel(string groupid)
        {
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            Dictionary<int, string> temp = GetAreaList(groupid);
            foreach (string value in temp.Values)
            {
                if (!result.ContainsKey(value)) result.Add(value, 0);
            }
            return result;
        }
    }
}